<template>
  <el-card>
    <el-row :gutter="20">
      <el-col :span="7">
        <el-input
          :placeholder="$t('userTable.placeholder')"
          type="text"
          v-model="queryForm.t.name"
        ></el-input>
      </el-col>
      <el-button type="primary" :icon="Search" @click="initGetUserList">{{
        $t('userTable.search')
      }}</el-button>
      <el-button type="primary" @click="handleDialogValue()">{{
        $t('userTable.adduser')
      }}</el-button>
    </el-row>

    <el-divider border-style="dashed" />

    <el-table :data="tableData" stripe style="width: 100%">
      <el-table-column
        v-for="(item, index) in options"
        :prop="item.prop"
        :label="$t(`userTable.${item.label}`)"
        :key="index"
        :width="item.width"
        :fixed="item.fixed"
        :show-overflow-tooltip="true"
      >
        <template v-slot="{ row }" v-if="item.prop === 'userStatus'">
          <el-switch
            v-model="row.userStatus"
            :active-value="'1'"
            :inactive-value="'0'"
            @click="changeUserState(row)"
          />
        </template>

        <template v-slot="{ row }" v-else-if="item.prop === 'createTime'">
          {{ $filters.filterUTCtimes(row.createTime) }}
        </template>

        <template #default="{ row }" v-else-if="item.prop === 'action'">
          <el-button
            type="primary"
            size="small"
            :icon="Edit"
            @click="handleDialogValue(row)"
          />
          <el-button
            type="danger"
            size="small"
            :icon="Delete"
            @click="delUser(row)"
          />
          <el-button
            type="warning"
            size="small"
            :icon="Setting"
            @click="handleJobDialogValue(row)"
          />
        </template>
      </el-table-column>
    </el-table>

    <el-pagination
      v-model:currentPage="queryForm.num"
      :page-sizes="[5, 10, 20, 50]"
      :page-size="queryForm.size"
      layout="total, sizes, prev, pager, next, jumper"
      :total="total"
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      @initUserList="initGetUserList"
    />
  </el-card>

  <Dialog
    v-model="dialogVisible"
    :dialogTitle="dialogTitle"
    v-if="dialogVisible"
    :dialogTableVal="dialogTableVal"
    @initUserList="initGetUserList"
  />

  <JobDialog
    v-if="jobDialogVisible"
    v-model="jobDialogVisible"
    :dialogTitle="jobDialogTitle"
    :dialogTableVal="jobDialogTableVal"
    :allJobList="allJobList"
    @initUserList="initGetUserList"
  />
</template>

<script setup>
import { Search, Edit, Delete, Setting } from '@element-plus/icons-vue'
import { getUser, getUserState, deleteUser } from '@/api/user.js'
import { getJobList } from '@/api/job.js'
import { listToList } from '@/util/transfer.js'
import { options } from './options'
import { useI18n } from 'vue-i18n'
import { ElMessage, ElMessageBox } from 'element-plus'
import { ref } from 'vue'
import Dialog from './components/dialog.vue'
import JobDialog from './components/job-dialog.vue'
import isNull from '@/util/filters'

const i18n = useI18n()
const queryForm = ref({
  t: {
    name: ''
  },
  sortList: null,
  page: 1,
  size: 10
})
// 用户
const total = ref(0)
const tableData = ref([])
const dialogVisible = ref(false)
const dialogTitle = ref('')
const dialogTableVal = ref({})
// 岗位
const jobDialogVisible = ref(false)
const jobDialogTitle = ref('')
const jobDialogTableVal = ref({})
const allJobList = ref([])

// 分页查询用户
const initGetUserList = async () => {
  const res = await getUser(queryForm.value)
  total.value = Number(res.total)
  tableData.value = res.dataList
}
initGetUserList()

// 查询岗位列表
const initGetJobList = async () => {
  const res = await getJobList({})
  allJobList.value = listToList(res)
}
initGetJobList()

// 修改页大小
const handleSizeChange = (pageSize) => {
  queryForm.value.num = 1
  queryForm.value.size = pageSize
  initGetUserList()
}

// 修改当前页
const handleCurrentChange = (page) => {
  queryForm.value.page = page
  initGetUserList()
}

// 更新用户状态
const changeUserState = async (info) => {
  await getUserState(info.id, info.userStatus)
  ElMessage({
    message: i18n.t('userMessage.updateSuccess'),
    type: 'success'
  })
}

// 新增或修改用户，显示对话框
const handleDialogValue = (row) => {
  if (isNull(row)) {
    dialogTitle.value = '添加用户'
    dialogTableVal.value = {}
  } else {
    dialogTitle.value = '编辑用户'
    dialogTableVal.value = JSON.parse(JSON.stringify(row))
  }
  dialogVisible.value = true
}

// 修改用户岗位，显示对话框
const handleJobDialogValue = (row) => {
  jobDialogTitle.value = '设置岗位'
  jobDialogTableVal.value = JSON.parse(JSON.stringify(row))
  jobDialogVisible.value = true
}

// 删除用户
const delUser = (row) => {
  ElMessageBox.confirm(i18n.t('userDialog.deleteTitle'), 'Warning', {
    confirmButtonText: 'OK',
    cancelButtonText: 'Cancel',
    type: 'warning'
  })
    .then(async () => {
      await deleteUser(row.id)
      ElMessage({
        type: 'success',
        message: 'Delete completed'
      })
      initGetUserList()
    })
    .catch(() => {
      ElMessage({
        type: 'info',
        message: 'Delete canceled'
      })
    })
}
</script>

<style lang="scss" scoped>
.el-pagination {
  margin-top: 20px;
  float: right;
}
:deep(.el-pagination.is-background .el-pager li:not(.is-disabled).is-active) {
  background-color: #ff9800 !important; //修改默认的背景色
}
</style>
